package com.hnevc.foodserver.service;

import com.hnevc.foodserver.bean.Category;
import com.hnevc.foodserver.dao.CategoryDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService {
    @Autowired
    CategoryDao categoryDao;
    public List<Category> list(){
        List<Category> all = categoryDao.findAll();
        return all;
    }

    public Category findone(Category category) {
        Category category1 = categoryDao.findOne(Example.of(category)).orElse(null);
        return category1;
    }

    public Category update(Category category) {
        return categoryDao.save(category);
    }

    public void deleteById(Integer id) {
        categoryDao.deleteById(id);
    }

    public Category add(Category category) {
        if(categoryDao.exists(Example.of(category))){
            return null;
        }else{
            return categoryDao.save(category);
        }
    }
}
